package com.adk.backend.controller;

import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxInventoryList;
import com.adk.backend.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓库管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    
    @Autowired
    private WarehouseService warehouseService;
    
    /**
     * 分页查询库存列表
     */
    @GetMapping("/inventory/list")
    public Result<PageResult<Map<String, Object>>> getInventoryList(
            @RequestParam(required = false) Long whId,
            @RequestParam(required = false) Long orderId,
            @RequestParam(required = false) String batchCode,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("whId", whId);
        params.put("orderId", orderId);
        params.put("batchCode", batchCode);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<Map<String, Object>> result = warehouseService.getInventoryList(params);
        return Result.success(result);
    }
    
    /**
     * 根据仓库ID查询库存
     */
    @GetMapping("/inventory/by-wh")
    public Result<List<LxInventoryList>> getInventoryByWh(@RequestParam Long whId) {
        List<LxInventoryList> list = warehouseService.getInventoryByWhId(whId);
        return Result.success(list);
    }
    
    /**
     * 入库操作
     */
    @PostMapping("/inventory/inbound")
    public Result<LxInventoryList> inbound(@RequestBody LxInventoryList inventory) {
        try {
            LxInventoryList result = warehouseService.inbound(inventory);
            return Result.success("入库成功", result);
        } catch (Exception e) {
            log.error("入库失败", e);
            return Result.error("入库失败：" + e.getMessage());
        }
    }
    
    /**
     * 出库操作
     */
    @PostMapping("/inventory/outbound")
    public Result<Boolean> outbound(@RequestParam Long inventoryId, @RequestParam Integer qty) {
        try {
            boolean result = warehouseService.outbound(inventoryId, qty);
            if (result) {
                return Result.success("出库成功", true);
            }
            return Result.error("出库失败");
        } catch (Exception e) {
            log.error("出库失败", e);
            return Result.error("出库失败：" + e.getMessage());
        }
    }
    
    /**
     * 库存调拨
     */
    @PostMapping("/inventory/transfer")
    public Result<Boolean> transfer(
            @RequestParam Long fromWhId,
            @RequestParam Long toWhId,
            @RequestBody List<Map<String, Object>> transferList) {
        try {
            boolean result = warehouseService.transfer(fromWhId, toWhId, transferList);
            if (result) {
                return Result.success("调拨成功", true);
            }
            return Result.error("调拨失败");
        } catch (Exception e) {
            log.error("调拨失败", e);
            return Result.error("调拨失败：" + e.getMessage());
        }
    }
}

